<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    width="70%"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="活动标题" prop="name">
        <el-input v-model="dataForm.name" placeholder="标题"/>
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="是否激活" prop="status">
            <el-switch
              :active-value=0
              :inactive-value=1
              v-model="dataForm.status">
            </el-switch>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-if="dataForm.status === 0" label="关联单位" prop="unitId">
            <el-select v-model="dataForm.unitId" style="width: 100%" filterable multiple placeholder="不做选择，默认关联所有单位" value="">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="活动介绍" prop="introduce">
        <tinymce v-if="this.isRouterAlive" ref="editor" v-model="dataForm.introduce"/>
      </el-form-item>
      <el-form-item label="" prop="def1" hidden>
        <el-input v-model="dataForm.def1" placeholder=""/>
      </el-form-item>
      <el-form-item label="" prop="def2" hidden>
        <el-input v-model="dataForm.def2" placeholder=""/>
      </el-form-item>
      <el-form-item label="" prop="def3" hidden>
        <el-input v-model="dataForm.def3" placeholder=""/>
      </el-form-item>
      <el-form-item label="" prop="def4" hidden>
        <el-input v-model="dataForm.def4" placeholder=""/>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary"  @click="dataFormSubmit()" :loading="this.isLoading">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import Tinymce from '@/components/Tinymce'

  export default {
    data () {
      return {
        visible: false,
        isRouterAlive: true,
        isLoading: false,
        isActivation: false,
        isUnit: false,
        dataForm: {
          id: 0,
          unitId: [],
          name: '',
          introduce: '',
          status: '',
          def0: '',
          def1: '',
          def2: ''
        },
        dataRule: {
          unitId: [
            {required: false, message: '单位id不能为空', trigger: 'blur'}
          ],
          name: [
            {required: true, message: '活动名称不能为空', trigger: 'blur'}
          ],
          introduce: [
            {required: true, message: '活动介绍不能为空', trigger: 'blur'}
          ],
          status: [
            {required: false, message: '状态  0：已激活  1：未激活不能为空', trigger: 'blur'}
          ],
          def0: [
            {required: false, message: '不能为空', trigger: 'blur'}
          ],
          def1: [
            {required: false, message: '不能为空', trigger: 'blur'}
          ],
          def2: [
            {required: false, message: '不能为空', trigger: 'blur'}
          ],
          def3: [
            {required: false, message: '不能为空', trigger: 'blur'}
          ],
          def4: [
            {required: false, message: '不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    props: {
      options: Array
    },
    components: {
      Tinymce
    },
    methods: {
      reload () {
        this.isRouterAlive = false
        this.$nextTick(() => (this.isRouterAlive = true))
      },
      init (id) {
        this.dataForm.id = id || 0
        this.reload()
        this.isLoading = false
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          this.dataForm.unitId = []
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/tra/activity/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.unitId = data.activity.unitId === '' ? [] : data.activity.unitId.split(',')
                this.dataForm.name = data.activity.name
                this.dataForm.introduce = data.activity.introduce
                this.dataForm.status = data.activity.status
                this.dataForm.def0 = data.activity.def0
                this.dataForm.def1 = data.activity.def1
                this.dataForm.def2 = data.activity.def2
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        let unitIds = this.dataForm.unitId.join(',')
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.isLoadingVisible()
            this.$http({
              url: this.$http.adornUrl(`/tra/activity/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'unitId': unitIds,
                'name': this.dataForm.name,
                'introduce': this.dataForm.introduce,
                'status': this.dataForm.status,
                'isDel': 0,
                'def0': this.dataForm.def0,
                'def1': this.dataForm.def1,
                'def2': this.dataForm.def2
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.isLoadingVisible()
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
                this.isLoadingVisible()
              }
            })
          }
        })
      },
      isLoadingVisible () {
        this.isLoading = !this.isLoading
      }
    }
  }
</script>
